const KEY = 'BDIHGDASKUGHDIASGDIUASDJKASNDKJSHDKSAHD'

export default class CacheStorageAdaptor {
  init() {
    // caches.delete('json-cache')
  }

  async query({ timestamp }) {
    // create a cache object
    const cache = await caches.open('json-cache')

    if (!cache) return

    const result = await cache.match(KEY + timestamp)

    if (result) {
      return {
        data: await result.json()
      }
    }
  }

  async insert({ timestamp, data }) {
    // create a cache object
    const cache = await caches.open('json-cache')

    if (!cache) return

    // create a Response
    const jsonResponse = new Response(data, {
      headers: {
        'content-type': 'application/json'
      }
    })

    return cache.put(KEY + timestamp, jsonResponse)
  }
}
